package com.florent37.runtimepermission3.slice;

import com.florent37.runtimepermission3.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import com.florent37.runtimepermission3.AppendText;
import com.florent37.runtime_permission.RuntimePermission;

public class TestActivitySlice extends AbilitySlice {
    private final static String WRITE_USER_STORAGE = "ohos.permission.WRITE_USER_STORAGE";

    private Text resultText;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_fragment_runtime_fragment_in);

        Text resultText = (Text)findComponentById(ResourceTable.Id_resultView);
        Component requestComponent = findComponentById(ResourceTable.Id_requestView);
        requestComponent.setClickedListener(component -> myMethod() );
    }

    public void myMethod(){
        RuntimePermission.askPermission((FractionAbility) TestActivitySlice.this.getAbility())
                .request(WRITE_USER_STORAGE)
                .onAccepted((result) -> {
                    //all permissions already granted or just granted

                    //your action
                    ToastDialog toastDialog = new ToastDialog(this);
                    toastDialog.setText("hasDenied :" + result.getAccepted());
                })
                .onDenied((result) -> {
                    AppendText.appendText(this, resultText, "hasForeverDenied :");
                    //the list of denied permissions
                    for (String permission : result.getDenied()) {
                        AppendText.appendText(this, resultText, permission);
                    }
                });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
